﻿using System;
using System.Text;
using Microsoft.SharePoint.Administration;

namespace Eneta.Portal.Common
{
    /// <summary>
    /// Wrapper class for loggers. The methods of this class are actually called from code. The class 
    /// gets current logger from resolver and calls wrapped methods on it.
    /// </summary>
    public static class Logger
    {
        /// <summary>
        /// Logs the informative message.
        /// </summary>
        /// <param name="message">The message to log.</param>
        /// <param name="source">The source of message.</param>
        public static void LogInfo(string message)
        {
            var trace = Environment.StackTrace;
            var messageBuilder = new StringBuilder();
            messageBuilder.Append(message);
            messageBuilder.Append(" Trace: ");
            messageBuilder.Append(trace);

            SPDiagnosticsService.Local.WriteTrace(0, 
                    new SPDiagnosticsCategory("Info", TraceSeverity.Verbose, EventSeverity.Information), 
                    TraceSeverity.Verbose, messageBuilder.ToString());
        }

        /// <summary>
        /// Logs the given warning.
        /// </summary>
        /// <param name="message">The message to log.</param>
        /// <param name="source">The source of message.</param>
        public static void LogWarning(string message)
        {
            var trace = Environment.StackTrace;
            var messageBuilder = new StringBuilder();
            messageBuilder.Append(message);
            messageBuilder.Append(" Trace: ");
            messageBuilder.Append(trace);

            SPDiagnosticsService.Local.WriteTrace(0, 
                new SPDiagnosticsCategory("Warning", TraceSeverity.Medium, EventSeverity.Warning),
                TraceSeverity.Medium, messageBuilder.ToString());
        }

        /// <summary>
        /// Logs the given exception.
        /// </summary>
        /// <param name="ex">The exception to log.</param>
        public static void LogException(Exception ex)
        {
            SPDiagnosticsService.Local.WriteTrace(0, 
                new SPDiagnosticsCategory("Exception", TraceSeverity.Unexpected, EventSeverity.Error), 
                TraceSeverity.Unexpected, ex.ToString());
        }       
    }
}
